package cn.careerx.app.common.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.SqlRow;

import libs.CodecNCrypto;

/**
 * 问答评论
 * @author YuNingbo
 * 2015年8月24日
 */
public class LeftOrRightComment implements Serializable{

	private static final long serialVersionUID = 3590606067723515241L;
	
	private String id;
	private String lorId;
	private String lorComment;
	private Long createAt;
	private String userId;
	private String userNickName;
	private String userHeadImg;
	
	public static LeftOrRightComment create(){
		return new LeftOrRightComment();
	}
	
	public static ArrayList<LeftOrRightComment> getLorCommentList(Database db,String lorId,int page,int pageSize) throws Exception{
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		List<SqlRow> rows = es.createSqlQuery("SELECT LRC.*,U.NICKNAME,U.PORTRAIT_KEY FROM CX_LOR_COMMENT LRC LEFT JOIN CX_USER U ON U.ID=LRC.USER_ID WHERE LOR_ID=? ORDER BY LRC.CREATE_AT DESC")
							  .setParameter(1, lorId)
							  .setFirstRow((page-1)*pageSize +1)
							  .setMaxRows(pageSize)
							  .findList();
		ArrayList<LeftOrRightComment> result = new ArrayList<LeftOrRightComment>();
		for(SqlRow row : rows){
			LeftOrRightComment lrc = LeftOrRightComment.create()
													   .setId(row.getString("ID"))
													   .setLorComment(row.getString("LOR_COMMENT"))
													   .setCreateAt(row.getDate("CREATE_AT").getTime())
													   .setUserHeadImg(row.getString("PORTRAIT_KEY"))
													   .setUserNickName(row.getString("NICKNAME"));
			result.add(lrc);
		}
		return result;
	}
	
	public static LeftOrRightComment addComment(Database db,LeftOrRightComment comment) throws Exception{
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		comment.setId(CodecNCrypto.UUID());
		int result = es.createSqlUpdate("INSERT INTO CX_LOR_COMMENT(ID,LOR_ID,LOR_COMMENT,CREATE_AT,USER_ID) VALUES (?,?,?,now(),?)")
		  .setParameter(1, comment.getId())
		  .setParameter(2, comment.getLorId())
		  .setParameter(3, comment.getLorComment())
		  .setParameter(4, comment.getUserId()).execute();
		if(result>0){
			comment.setCreateAt(new Date().getTime());
			return comment;
		}else{
			return null;
		}
	}
	
	/*******************************Setter And Getters*******************************************/
	public String getId() {
		return id == null ? "" : id;
	}
	public LeftOrRightComment setId(String id) {
		this.id = id;
		return this;
	}
	public String getLorId() {
		return lorId == null ? "" : lorId;
	}
	public LeftOrRightComment setLorId(String lorId) {
		this.lorId = lorId;
		return this;
	}
	public String getLorComment() {
		return lorComment == null ? "" : lorComment;
	}
	public LeftOrRightComment setLorComment(String lorComment) {
		this.lorComment = lorComment;
		return this;
	}
	
	public Long getCreateAt() {
		return createAt == null ? 0 : createAt;
	}

	public LeftOrRightComment setCreateAt(Long createAt) {
		this.createAt = createAt;
		return this;
	}

	public String getUserNickName() {
		return userNickName == null ? "" : userNickName;
	}
	public LeftOrRightComment setUserNickName(String userNickName) {
		this.userNickName = userNickName;
		return this;
	}
	public String getUserHeadImg() {
		return userHeadImg == null ? "" : userHeadImg;
	}
	public LeftOrRightComment setUserHeadImg(String userHeadImg) {
		this.userHeadImg = userHeadImg;
		return this;
	}

	public String getUserId() {
		return userId == null ? "" : userId;
	}

	public LeftOrRightComment setUserId(String userId) {
		this.userId = userId;
		return this;
	}
}
